package com.welike.shibo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.welike.shibo.eneity.Admin;
import com.welike.shibo.enums.ResultEnum;
import com.welike.shibo.exception.BaseException;
import com.welike.shibo.mapper.AdminMapper;
import com.welike.shibo.service.*;
import com.welike.shibo.utils.PasswordEncrypt;
import com.welike.shibo.vo.IndexDataVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019-08-20 00:40
 */
@Slf4j
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private UserService userService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private ZanService zanService;

    @Override
    public Admin login(String username, String password) throws Exception {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            throw new BaseException(ResultEnum.USERNAME_PASSWORD_IS_EMPTY);
        }
        String pwd = PasswordEncrypt.encodeByMd5(password);
        Admin admin = adminMapper.queryAllByUsernameAndPassword(username, pwd);
        log.info("adminMapper login ==> {}",admin);
        if (null == admin) {
            throw new BaseException(ResultEnum.USERNAME_PASSWORD_ERROR);
        }
        return admin;
    }

    @Override
    public IndexDataVO queryIndexData() {
        IndexDataVO indexData = new IndexDataVO();
        indexData.setCommentCount(commentsService.countByComment());
        indexData.setUserCount(userService.countByUser());
        indexData.setVideoCount(videoService.countByVideo());
        indexData.setZanCount(zanService.countByZan());
        return indexData;
    }
}
